<?php
/** 
 * Created by RisingSun. 
 * User: MarkSpace 
 * Date: 20231021
 * Time: 11:05 
 * 资产管理
 */
namespace app\finance\controller;

use  Mscore\Core\Controller;
use  app\finance\model\Assets as AssetsModel;
use  app\finance\model\Withdraw as WithdrawModel;
use  app\finance\model\Recharge as RechargeModel;
use  app\finance\model\Common as CommonModel;
use  Mscore\Core\Lib\ArrayMethod;
use  app\admin\model\Login as LoginModel;
use  Mscore\Core\Lang;

class AssetsController extends Controller{

    private $model;
    private $login;
    private $error;
    private $success;
    private $language;
    /**
     * 初始化自动连接方法
     * @param array $sysmod 
     * @param array $where 
    */
    public function __construct()  
    {    
        $this->login = (new LoginModel)->isAuth();
        if(isset($this->login['code'])&&$this->login['code']==401){
            echo json_encode($this->login);
            Header("Location:".config('server.login_url'));
            exit;
        }
        $this->model = new AssetsModel;
        $this->language = isset($_COOKIE['language'])?$_COOKIE['language']:'default';
        Lang::init($this->language);
        $this->success=Lang::get('Success');
        $this->error=Lang::get('Error');
    }   

    /**
     * 资产
     * @param array $add 
     * @param array $where 
     */ 
    public function assets(){
        $params = post();
        switch (true) {
            case $params:
                $status = $this->model->save($params);
                switch ($status) {
                    case true:
                        $this->echoJson($this->success);
                        break;
                    default:
                        $this->echoJson($this->error);
                        break;
                }
                break;
            default:
                if(get('aid')){
                    $datainfo =$this->model->info(get('aid'));
                }
                $datainfo['username']=isset($datainfo['username'])?$datainfo['username']:'';
                $datainfo['realname']=isset($datainfo['realname'])?$datainfo['realname']:'';
                $datainfo['mobile']=isset($datainfo['mobile'])?$datainfo['mobile']:'';
                $datainfo['email']=isset($datainfo['email'])?$datainfo['email']:'';
                $datainfo['cash']=isset($datainfo['cash'])?$datainfo['cash']:0;
                $datainfo['amount']=isset($datainfo['amount'])?$datainfo['amount']:0;
                $datainfo['integra']=isset($datainfo['integra'])?$datainfo['integra']:0;
                $datainfo['credit']=isset($datainfo['credit'])?$datainfo['credit']:0;

                $action = get('action');
                $this->assign('action', $action);
                $this->assign('datainfo',$datainfo);
                $this->display('assets_list.html');
                break;
        }
    }

    /**
     * 提现列表
     * @param array $withdraw 
     * @param array $where 
     */ 
    public function withdraw(){  
        $page = post('page',1);
        $limit = post('limit',10);
        $aid = post('aid');
        $where = 'a.dels<1 and a.aid= '.$aid;
        $datalist = (new WithdrawModel())->withdraw($where,$page,$limit);
        $data = [
            "code" => 200,
            "data" => $datalist['datalist'],
            "count"=> $datalist['total'],
            "msg" => 'ok'
        ];
        $this->echojson($data);
    }

    /**
     * 支付提现
     * @param array $withdraw 
     * @param array $where 
     */ 
    public function addwithdraw(){
        $params = post();
        switch (true) {
            case $params:
                $status = (new WithdrawModel())->save($params,$this->login['info']);
                switch ($status) {
                    case true:
                        $this->echoJson($this->success);
                        break;
                    default:
                        $this->echoJson($this->error);
                        break;
                }
                break;
            default:
                $payment = (new CommonModel())->getBaseinfo('keyname="payment"','content')['content'];
                $currency='';$gift=0;$rate=1;$withgift=1;
                if(!empty($payment)){
                	$currency = $payment['currency'];
                    $feerate = $payment['withdrawal'];
                    $withgift = $payment['withgift'];
                    $rate = (new CommonModel())->getexchangeinfo(' codes= "'.$currency.'"','rate')['rate']; //平臺貨幣匯率
                }
                $aid=get('aid',0);
                if(!empty($aid)){
                	$user=(new CommonModel())->getAccount('aid='.$aid,'amount,integra');
                }
                $ratebase = (new CommonModel())->getexchangeinfo(' status>0 and dels<1 and defualt>0 ','rate'); //標準貨幣匯率
                $datainfo['aid']=isset($datainfo['aid'])?$datainfo['aid']:$aid;
                $datainfo['type']=isset($datainfo['type'])?$datainfo['type']:2;
                $datainfo['money']=isset($datainfo['money'])?$datainfo['money']:0;
                $datainfo['remoney']=isset($datainfo['remoney'])?$datainfo['remoney']:0;
                $datainfo['fee']=isset($datainfo['fee'])?$datainfo['fee']:round($datainfo['money']*$feerate/100);
                $datainfo['status']=isset($datainfo['status'])?$datainfo['status']:1;
                $datainfo['currency']=isset($datainfo['currency'])&&!empty($datainfo['currency'])?$datainfo['currency']:$currency;
                $datainfo['codes']=$currency;
                $datainfo['rate']=isset($datainfo['rate'])&&!empty($datainfo['rate'])?$datainfo['rate']:($ratebase['rate']/$rate)*$rate;
                $datainfo['file_url']=isset($datainfo['file_url'])?$datainfo['file_url']:'';
                $datainfo['payment']=isset($datainfo['payment'])?$datainfo['payment']:'';
                $datainfo['status']=isset($datainfo['status'])?$datainfo['status']:1;

                $exchange = (new CommonModel())->getexchange(' status>0 and dels<1 ','id,name, rate, codes');

                $bank = (new CommonModel())->bank(' aid='.$aid);

                $this->assign('userate', $rate);
                $this->assign('feerate', $feerate/100);
                $this->assign('ratebase', $ratebase['rate']);
                $this->assign('withgift', $withgift);
                $this->assign('bank', $bank);
                $this->assign('exchange', $exchange);
                $this->assign('user',$user);
                $this->assign('datainfo',$datainfo);
                $this->display('paywithdraw.html');
                break;
        }
    }

    /**
     * 支付提现
     * @param array $paywithdraw 
     * @param array $where 
     */ 
    public function paywithdraw(){
        $params = post();
        switch (true) {
            case $params:
                $status = (new WithdrawModel())->examine($params,$this->login['info']);
                switch ($status) {
                    case true:
                        $this->echoJson($this->success);
                        break;
                    default:
                        $this->echoJson($this->error);
                        break;
                }
                break;
            default:
                if(get('id')){
                    $datainfo =(new WithdrawModel())->drawinfo(get('id'));
                    $user=(new CommonModel())->getAccount('aid='.$datainfo['aid'],'amount,integra');
                }else{
                	$datainfo = [];
                }
                $bank = (new CommonModel())->bank(' aid='.$datainfo['aid']);
                $this->assign('bank', $bank);
                $this->assign('user', $user);
                $this->assign('datainfo',$datainfo);
                $this->display('editwithdraw.html');
                break;
        }
    }

    /**
     * 充值列表
     * @param array $rechargelist 
     * @param array $where 
     */ 
    public function rechargelist(){  
        $page = post('page',1);
        $limit = post('limit',10);
        $aid = post('aid');
        $where = 'a.dels<1 and a.aid= '.$aid;
        $datalist = (new RechargeModel())->list($where,$page,$limit);
        $data = [
            "code" => 200,
            "data" => $datalist['datalist'],
            "count"=> $datalist['total'],
            "msg" => 'ok'
        ];
        $this->echojson($data);
    }

    /**
     * 充值
     * @param array $recharge 
     * @param array $where 
     */ 
    public function recharge(){
        $params = post();
        switch (true) {
            case $params:
                $params['method']=2;
                $status = (new RechargeModel())->save($params, $this->login['info']);
                switch ($status) {
                    case true:
                        $this->echoJson($this->success);
                        break;
                    default:
                        $this->echoJson($this->error);
                        break;
                }
                break;
            default:
                $payment = (new CommonModel())->getBaseinfo('keyname="payment"','content')['content'];
                $currency='';$gift=0;$rate=1;
                if(!empty($payment)){
                	$currency = $payment['currency'];
                    $gift = $payment['gift'];
                    $rate = (new CommonModel())->getexchangeinfo(' codes= "'.$currency.'"','rate')['rate']; //平臺貨幣匯率
                }
                $ratebase = (new CommonModel())->getexchangeinfo(' status>0 and dels<1 and defualt>0 ','rate'); //標準貨幣匯率
                $datainfo['aid']=isset($datainfo['aid'])?$datainfo['aid']:get('aid',0);
                $datainfo['money']=isset($datainfo['money'])?$datainfo['money']:0;
                $datainfo['integra']=isset($datainfo['integra'])?$datainfo['integra']:0;
                $datainfo['remoney']=isset($datainfo['remoney'])?$datainfo['remoney']:0;
                $datainfo['reintegra']=isset($datainfo['reintegra'])?$datainfo['reintegra']:0;
                $datainfo['currency']=isset($datainfo['currency'])?$datainfo['currency']:$currency;
                $datainfo['rate']=isset($datainfo['rate'])&&!empty($datainfo['rate'])?$datainfo['rate']:($ratebase['rate']/$rate)*$rate;
                $datainfo['file_url']=isset($datainfo['file_url'])?$datainfo['file_url']:'';
                $datainfo['status']=isset($datainfo['status'])?$datainfo['status']:1;
                $datainfo['type']=isset($datainfo['type'])?$datainfo['type']:1;
                unset($payment);
                $exchange = (new CommonModel())->getexchange(' status>0 and dels<1 ','id,name, rate, codes');
                $this->assign('exchange', $exchange);
                $this->assign('userate', $rate);
                $this->assign('ratebase', $ratebase['rate']);
                $this->assign('currency', $currency);
                $this->assign('gift', $gift);
                $this->assign('datainfo',$datainfo);
                $this->display('recharge.html');
                break;
        }
    }

    /**
     * 充值
     * @param array $editrech 
     * @param array $where 
     */ 
    public function editrech(){
        $params = post();
        switch (true) {
            case $params:
                $status = (new RechargeModel())->examine($params,$this->login['info']);
                switch ($status) {
                    case true:
                        $this->echoJson($this->success);
                        break;
                    default:
                        $this->echoJson($this->error);
                        break;
                }
                break;
            default:
                if(get('id')){
                    $datainfo =(new RechargeModel())->recharge(get('id'));
                }else{
                	$datainfo = [];
                }
                $this->assign('datainfo',$datainfo);
                $this->display('editrech.html');
                break;
        }
    }

    /**
     * 账户列表
     * @param array $list 
     * @param array $where 
     */ 
    public function list(){  
        //文章类
        $post = post();
        switch (true) {
            case $post:
                $page = post('page',1);
                $limit = post('limit',10);
                $where = ' a.dels<1 ';
                if($this->login['info']['ranges']<1){
                    $where .= ' and (a.pid = '.$this->login['info']['aid'].' or a.aid='.$this->login['info']['aid'].')';
                }
                if(post('type')){
                    $where .= ' and a.grade = "'.post('type').'"';
                }
                if(post('username')){
                    $where .= ' and a.username like "%'.post('username').'%"';
                }
                if(post('realname')){
                    $where .= ' and a.realname like "%'.post('realname').'%"';
                }
                if(post('mobile')){
                    $where .= ' and a.mobile like "%'.post('mobile').'%"';
                }
                if(post('roletype')){
                    $where .= ' and a.roletype = '.post('roletype');
                }
                $datalist = $this->model->list($where,$page,$limit);
                $data = [
                    "code" => 200,
                    "data" => $datalist['datalist'],
                    "count"=> $datalist['total'],
                    "msg" => 'ok'
                ];
                $this->echojson($data);
                break;
            default:
                $rolelist = (new CommonModel())->getrole('dels<1 and status>0');
                $type=!empty(get('type'))?get('type'):'';
                $this->assign('type', $type);
                $this->assign('rolelist', $rolelist);
                $this->display('list.html');
                break;
        }
    }  
}